package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.ws.rs.DefaultValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.recruit.JobAddCo;
import com.engineering.elink.co.recruit.JobCo;
import com.engineering.elink.co.recruit.JobSearchCo;
import com.engineering.elink.common.DictConstant;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.service.JobService;
import com.engineering.elink.util.ResJsonUtil;

@RestController
@CrossOrigin
@RequestMapping("/api/job")
@Api(value="职位相关接口")
public class JobController {
	
	@Autowired
	private JobService jobService;
	
	/**
	 * 获取所有工作性质
	 */
	@RequestMapping(value = "/getJobNature", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有工作性质")
	public Map<String, Object> getJobNature() {
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getDictCategoryByCode(DictConstant.JOB_NATURE,0));
	}
	
	/**
	 * 获取所有最低学历
	 */
	@RequestMapping(value = "/getJobEducation", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有最低学历")
	public Map<String, Object> getJobEducation() {
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getDictCategoryByCode(DictConstant.JOB_EDUCATION,3));
	}
	
	/**
	 * 获取所有工作年限
	 */
	@RequestMapping(value = "/getJobExperience", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有工作年限")
	public Map<String, Object> getJobExperience() {
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getDictCategoryByCode(DictConstant.JOB_EXPERIENCE,2));
	}
	
	/**
	 * 获取职能一级分类
	 */
	@RequestMapping(value = "/getFunctionsTopCategory", method = RequestMethod.GET)
	@ApiOperation(value = "获取职能一级分类")
	public Map<String, Object> getFunctionsTopCategory() {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getFunctionsTopCategory());
	}
	
	/**
	 * 获取职能详情分类
	 */
	@RequestMapping(value = "/getFuncDetailCategory", method = RequestMethod.GET)
	@ApiOperation(value = "获取职能详情分类")
	public Map<String, Object> getFuncDetailCategory(@ApiParam(value = "职能一级分类 ", required = true) @RequestParam("ftcId") int ftcId) {
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobService.getFuncDetailCategoryByFtcId(ftcId));
	}
	
	/**
	 * 获取语言
	 */
	@RequestMapping(value = "/getLanguage", method = RequestMethod.GET)
	@ApiOperation(value = "获取语言")
	public Map<String, Object> getLanguage() {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getLanguage());
	}
	
	/**
	 * 获取专业一级分类
	 */
	@RequestMapping(value = "/getMajorTopCategory", method = RequestMethod.GET)
	@ApiOperation(value = "获取专业一级分类")
	public Map<String, Object> getMajorTopCategory() {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getMajorTopCategory());
	}
	
	/**
	 * 获取专业二级分类
	 */
	@RequestMapping(value = "/getMajorScByMdcId", method = RequestMethod.GET)
	@ApiOperation(value = "获取专业二级分类")
	public Map<String, Object> getMajorScByMdcId(@ApiParam(value = "专业一级分类ID ", required = true) @RequestParam("mdcId") int mdcId) {
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobService.getMajorScByMdcId(mdcId));
	}
	
	/**
	 * 获取专业详情分类
	 */
	@RequestMapping(value = "/getProfessionalByMscId", method = RequestMethod.GET)
	@ApiOperation(value = "获取专业详情分类")
	public Map<String, Object> getProfessionalByMscId(@ApiParam(value = "专业二级分类ID ", required = true) @RequestParam("mscId") int mscId) {
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobService.getProfessionalByMscId(mscId));
	}
	
	/**
	 * 获取语言等级
	 */
	@RequestMapping(value = "/getLanguageGrade", method = RequestMethod.GET)
	@ApiOperation(value = "获取语言等级")
	public Map<String, Object> getLanguageGrade() {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,jobService.getLanguageGrade());
	}
	
	/**
	 * 发布职位
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	@ApiOperation(value = "发布职位")
	public Map<String, Object> add(@ModelAttribute @Valid JobAddCo addCo,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
			
			if(userInfo.get("userType")!=null && (int)userInfo.get("userType")==2){
				//只有个人用户才能够投递//1个人2企业
				return jobService.add(addCo,userId);
			}else{
				return ResJsonUtil.getMap(ErrorType.AUTHORITY_ERROR);
			}
			
		}
		
		return ResJsonUtil.getMap(ErrorType.NEED_LOGIN);
		
	}
	
	/**
	 * 暂停发布
	 */
	@RequestMapping(value = "/pause", method = RequestMethod.PUT)
	@ApiOperation(value = "暂停发布")
	public Map<String, Object> pause(@ApiParam(value = "职位ID ", required = true) @RequestParam("jobId") String jobId) {
		return jobService.pause(jobId);
	}
	
	/**
	 * 单个职位详情
	 * @since JDK 1.8
	 */
	@RequestMapping(value="/get",method = RequestMethod.GET)
	@ApiOperation(value = "职位详情")
	public Map<String, Object> get(
			@ApiParam(value="职位id",required=true) @RequestParam(value = "jobId",required=true)String jobId,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null)
			userId = userInfo.get("userId").toString();
		
		return jobService.get(jobId,userId);
	}
	
	/**
	 * 修改职位
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ApiOperation(value = "修改职位")
	public Map<String, Object> update(@ApiParam(value="职位id",required=true) @RequestParam(value = "jobId",required=true)String jobId,
			@ModelAttribute @Valid JobAddCo addCo,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return jobService.update(jobId,addCo,userId);
	}
	
	/**
	 * 批量删除职位
	 */
	@RequestMapping(value = "/deleteJob", method = RequestMethod.DELETE)
	@ApiOperation(value = "批量删除职位")
	public Map<String, Object> deleteJob(@ApiParam(value = "职位ID字符串(示例：a,b,c)", required = true) @RequestParam("jobIds") String jobIds) {
		return jobService.deleteJob(jobIds);
	}
	
	/**
	 * 刷新职位
	 */
	@RequestMapping(value = "/refreshJob", method = RequestMethod.PUT)
	@ApiOperation(value = "刷新职位")
	public Map<String, Object> refreshJob(@ApiParam(value = "职位ID字符串(示例：a,b,c)", required = true) @RequestParam("jobIds") String jobIds) {
		return jobService.refreshJob(jobIds);
	}
	
	/**
	 * 职位列表
	 */
	@RequestMapping(value="/getList",method = RequestMethod.GET)
	@ApiOperation(value = "职位列表")
	public Map<String, Object> getList(@ModelAttribute @Valid JobCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			 userId =  userInfo.get("userId").toString();
		}
		
		return jobService.getJobsList(co,userId);
	}
	
	/**
	 * 该公司的其他职位
	 */
	@RequestMapping(value="/getCompanyJob",method = RequestMethod.GET)
	@ApiOperation(value = "该公司的其他职位")
	public Map<String, Object> getCompanyJob(@ApiParam(value = "职位ID字符串", required = true) @RequestParam("jobId") String jobId,
			@ApiParam(value="条数",required=false,defaultValue="6")@RequestParam("count")@DefaultValue("6") int count){
		
//		@SuppressWarnings("unchecked")
//		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
//		String userId =  userInfo.get("userId").toString();
		
		return jobService.getCompanyJob(jobId,count,"");
	}
	
	/**
	 * 相似职位
	 */
	@RequestMapping(value="/getSameJob",method = RequestMethod.GET)
	@ApiOperation(value = "相似职位")
	public Map<String, Object> getSameJob(@ApiParam(value = "职位ID字符串", required = true) @RequestParam("jobId") String jobId,
			@ApiParam(value="条数",required=false,defaultValue="6")@RequestParam("count")@DefaultValue("6") int count){
		
//		@SuppressWarnings("unchecked")
//		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
//		String userId =  userInfo.get("userId").toString();
		
		return jobService.getSameJob(jobId,count,"");
	}
	
	/**
	 * 立即投递
	 */
	@RequestMapping(value="/deliveryJob",method = RequestMethod.POST)
	@ApiOperation(value = "立即投递")
	public Map<String, Object> deliveryJob(
			@ApiParam(value = "职位ID字符串", required = true)@RequestParam("jobId") String jobId,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
			
			if(userInfo.get("userType")!=null && (int)userInfo.get("userType")==1){
				//只有个人用户才能够投递//1个人2企业
				return jobService.deliveryJob(jobId,userId);
			}else{
				return ResJsonUtil.getMap(ErrorType.AUTHORITY_ERROR);
			}
		}
		
		return ResJsonUtil.getMap(ErrorType.NEED_LOGIN);
		
	}
	
	/**
	 * 投递的简历
	 */
	@RequestMapping(value="/getMyDeliveryJob",method = RequestMethod.GET)
	@ApiOperation(value = "投递的简历")
	public Map<String, Object> getMyDeliveryJob(
			@ApiParam(value = "关键词搜索公司", required = false)@RequestParam(value="fullName",required=false) String fullName,
			@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		
		if(userInfo!=null && userInfo.get("userId")!=null)
			userId =  userInfo.get("userId").toString();
		
		return jobService.getMyDeliveryJob(fullName,co,userId);
	}
	
	/**
	 * 批量意向公司
	 */
	@RequestMapping(value = "/targetJob", method = RequestMethod.PUT)
	@ApiOperation(value = "批量意向公司")
	public Map<String, Object> targetJob(
			@ApiParam(value = "职位ID字符串(示例：a,b,c)", required = true) @RequestParam("jobIds") String jobIds,HttpServletRequest request) {
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
			
			return jobService.targetJob(jobIds,userId);
		}
		
		return ResJsonUtil.getMap(ErrorType.NEED_LOGIN);
		
	}
	
	/**
	 * 收藏的职位
	 */
	@RequestMapping(value="/getMyCollectionJob",method = RequestMethod.GET)
	@ApiOperation(value = "收藏的职位")
	public Map<String, Object> getMyCollectionJob(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return jobService.getMyCollectionJob(co,userId);
	}
	
	/**
	 * 简历管理－搜索职位
	 */
	@RequestMapping(value="/getJobList",method = RequestMethod.GET)
	@ApiOperation(value = "简历管理－搜索职位")
	public Map<String, Object> getJobList(@ModelAttribute @Valid JobSearchCo co){
		
//		@SuppressWarnings("unchecked")
//		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
//		String userId =  userInfo.get("userId").toString();
		
		return jobService.getSearchJobList(co,"");
	}
	
	/**
	 * 获取热门搜索词
	 */
	@RequestMapping(value="/getKeyWord",method = RequestMethod.GET)
	@ApiOperation(value = "获取热门搜索词")
	public Map<String, Object>getKeyWord(
			@ApiParam(value="显示条数,默认为6条",required=false,defaultValue="6")
			@RequestParam(value = "count",required=false,defaultValue="6")int count){
		
		return jobService.getKeyWord(count);
	}
	
}
